package com.itboye.sunsunhome.www.aq;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

public class AqMessageNotification {

	private static final String NOTIFICATION_TAG = "AqMessage";

	public static void notify(final Context context,
			final String exampleString, final int number) {
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>222222");
		final Resources res = context.getResources();
		final Bitmap picture = BitmapFactory.decodeResource(res,
				R.drawable.push);
		final String ticker = exampleString;
		final String title = "森森AQ";
		final String text = exampleString;

		final Intent appIntent = new Intent(Intent.ACTION_MAIN);
		appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		appIntent.setComponent(new ComponentName(context.getPackageName(),
				context.getPackageName() + ".StartActivity"));
		// 关键的一步，设置启动模式
		appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

		final NotificationCompat.Builder builder = new NotificationCompat.Builder(
				context)
				.setDefaults(Notification.DEFAULT_ALL)
				.setSmallIcon(R.drawable.push)
				.setContentTitle(title)
				.setContentText(text)
				.setPriority(NotificationCompat.PRIORITY_DEFAULT)
				.setLargeIcon(picture)
				.setTicker(ticker)
				.setWhen(System.currentTimeMillis())
				.setNumber(number)
				.setContentIntent(
						PendingIntent.getActivity(context, 0, appIntent,
								PendingIntent.FLAG_UPDATE_CURRENT))
				.setAutoCancel(true);

		notify(context, builder.build());
	}

	@TargetApi(Build.VERSION_CODES.ECLAIR)
	private static void notify(final Context context,
			final Notification notification) {
		final NotificationManager nm = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
			nm.notify(NOTIFICATION_TAG, 0, notification);
		} else {
			nm.notify(NOTIFICATION_TAG.hashCode(), notification);
		}
	}

	@TargetApi(Build.VERSION_CODES.ECLAIR)
	public static void cancel(final Context context) {
		final NotificationManager nm = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
			nm.cancel(NOTIFICATION_TAG, 0);
		} else {
			nm.cancel(NOTIFICATION_TAG.hashCode());
		}
	}
}